function hfile = vmr_PrepareForReco(hfile, sval, bval)
% VMR::PrepareForReco  - prepare border for reconstruction
%
% FORMAT:       [vmr] = vmr.PrepareForReco([sval, bval])
%
% Input fields:
%
%       sval        an optional segmentation value (default: 240)
%       bval        an optional border value (default: 235)
%
% Output fields:
%
%       vmr         prepared VMR
%
% NOTE: this function does NOT yet work as expected !!

% Version:  v0.7b
% Build:    7090216
% Date:     Sep-02 2007, 4:42 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'vmr')
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
bc = bvqxfile_getcont(hfile.L);
if nargin < 2 || ...
   ~isa(sval, 'double') || ...
    numel(sval) ~= 1 || ...
    isnan(sval) || ...
    sval < 1 || ...
    sval > 255
    sval = 240;
else
    sval = fix(real(sval));
end
if nargin < 3 || ...
   ~isa(bval, 'double') || ...
    numel(bval) ~= 1 || ...
    isnan(bval) || ...
    bval < 1 || ...
    bval > 255
    bval = 235;
else
    bval = fix(real(bval));
end

% get VMRdata
vmrd = bc.VMRData(:, :, :);

% replace bval with sval
vmrd(vmrd == bval) = sval;

% create boolean matrix with non matching color
vmrb = (vmrd ~= sval);

% find border by notting array with dilation operator
vmrd(~vmrb & conv3d(vmrb, 2)) = bval;

% back in VMR
bc.VMRData = vmrd;
bvqxfile_setcont(hfile.L, bc);
